from datetime import date

from qcloud_cos import CosConfig, CosS3Client

# 文档地址： https://cloud.tencent.com/document/product/436/12269


class TenCos:
    def __init__(self):
        secret_id = "AKIDEG4d50OdEZkicO5Tdu9KcfU6axCSoLYX"  # 替换为用户的 secretId
        secret_key = "0mLpY2WnVXMSKxNYS5W1GGBuT0HLCn9S"  # 替换为用户的 secretKey
        region = "ap-nanjing"
        config = CosConfig(
            Region=region, SecretId=secret_id, SecretKey=secret_key  # 替换为用户的 Region
        )
        # 2. 获取客户端对象
        self.client = CosS3Client(config)

    def save_user_pic(self, filename, file, default_path="user_pic"):
        filename = "/".join([default_path, date.today().strftime("%Y-%m"), filename])
        file_url = (
            "https://qingdeng123-1257113111.cos.ap-nanjing.myqcloud.com/" + filename
        )
        response = self.client.put_object(
            Bucket="qingdeng123-1257113111", Body=file, Key=filename, EnableMD5=False
        )
        if response["ETag"]:
            return "ok", file_url
        else:
            return "", ""
